package com.handler.filter;

import com.handler.game.BaseHandler;
import lombok.Data;

/**
 * @author : gameZhao
 * @version V1.0
 * @Project: disruptorDemo
 * @Package com.handler.filter
 * @Description: TODO
 * @date Date : 2021年12月28日 15:11
 */
@Data
public class HandlerFilterChain {
    
    private HandlerFilter handlerFilter;
    
    private HandlerFilterChain next;
    public HandlerFilterChain() {
    }
    public HandlerFilterChain(HandlerFilter handlerFilter) {
        this.handlerFilter = handlerFilter;
    }
    
    public void registerFilter(HandlerFilter handlerFilter) {
        //第一个handler
        if (this.handlerFilter == null) {
            this.handlerFilter = handlerFilter;
            return;
        }
        //后续链
        if (next == null) {
            this.next = new HandlerFilterChain(handlerFilter);
            return;
        }
        registerFilter(handlerFilter);
    }
    
    public boolean process(BaseHandler baseHandler) {
        if (handlerFilter.filter(baseHandler)) {
            return false;
        }
        if (next != null) {
            next.process(baseHandler);
        }
        return true;
    }
    
    ;
}
